package com.soft.jdbc.student.service;

import com.soft.jdbc.student.dao.StudentDAO;
import com.soft.jdbc.student.entity.Student;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/6/6
 */
public class StudentService {
    private StudentDAO studentDao = new StudentDAO();
    private Scanner sc = new Scanner(System.in);

    // 登录验证
    public boolean login() {
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        return "张无忌".equals(username) && "8888".equals(password);
    }

    // 显示主菜单
    public void showMainMenu() {
        System.out.println("============请选择操作键============");
        System.out.println("1、统计学生人数");
        System.out.println("2、查看学生名单");
        System.out.println("3、按学号查询学生姓名");
        System.out.println("4、按姓名查询学生信息");
        System.out.println("5、修改学生出生日期");
        System.out.println("6、删除学生记录");
        System.out.println("0、退出");
    }

    // 统计学生人数
    public void countStudents() {
        System.out.println("============查询操作============");
        System.out.println("操作键：1");
        int count = studentDao.countStudents();
        System.out.println("当前共有 " + count + " 名学生");
    }

    // 查看所有学生
    public void listAllStudents() {
        System.out.println("============查询操作============");
        System.out.println("操作键：2");
        System.out.println("____________学生列表____________");
        System.out.println("学号\t\t姓名\t\t性别\t\t年级\t\t电话\t\t\t\t邮箱\t\t\t\t出生日期");

        List<Student> students = studentDao.getAllStudents();
        for (Student student : students) {
            System.out.println(student);
        }
    }

    // 按学号查询学生
    public void queryStudentById() {
        System.out.println("============查询操作============");
        System.out.println("操作键：3");
        System.out.println("请输入学生学号：");
        String id = sc.nextLine();

        Student student = studentDao.getStudentById(id);
        if (student != null) {
            System.out.println("查询成功！");
            System.out.println(id + " 号的学生姓名为：" + student.getName() + "，所在年级：" + student.getGrade());
        } else {
            System.out.println("未找到学号为 " + id + " 的学生");
        }
    }

    // 按姓名查询学生
    public void queryStudentsByName() {
        System.out.println("============查询操作============");
        System.out.println("操作键：4");
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();

        List<Student> students = studentDao.getStudentsByName(name);
        if (!students.isEmpty()) {
            System.out.println("____________学生列表____________");
            System.out.println("学号\t\t姓名\t\t性别\t\t年级\t\t电话\t\t\t\t邮箱\t\t\t\t出生日期");
            for (Student student : students) {
                System.out.println(student);
            }
        } else {
            System.out.println("未找到姓名为 " + name + " 的学生");
        }
    }

    // 修改学生出生日期
    public void updateStudentBirthday() {
        System.out.println("============修改操作============");
        System.out.println("操作键：5");
        System.out.println("请输入学生学号：");
        String id = sc.nextLine();
        System.out.println("请输入修改后的生日(yyyy-MM-dd)：");
        String dateStr = sc.nextLine();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date newBirthday = sdf.parse(dateStr);

            boolean success = studentDao.updateStudentBirthday(id, new java.sql.Date(newBirthday.getTime()));
            if (success) {
                System.out.println("修改成功！");
            } else {
                System.out.println("修改失败，请检查学号是否正确");
            }
        } catch (ParseException e) {
            System.out.println("日期格式不正确，请使用yyyy-MM-dd格式");
        }
    }

    // 删除学生记录
    public void deleteStudent() {
        System.out.println("============删除操作============");
        System.out.println("操作键：6");
        System.out.println("请输入学生学号：");
        String id = sc.nextLine();

        Student student = studentDao.getStudentById(id);
        if (student == null) {
            System.out.println("未找到学号为 " + id + " 的学生");
            return;
        }

        System.out.println("确认要删除学号为" + id + "，姓名为" + student.getName() + "的学生信息吗？(Y/N)");
        String confirm = sc.nextLine();

        if ("Y".equalsIgnoreCase(confirm)) {
            boolean success = studentDao.deleteStudent(id);
            if (success) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败");
            }
        } else {
            System.out.println("已取消删除操作");
        }
    }

    public void run() {
        if (!login()) {
            System.out.println("用户名或密码错误！");
            return;
        }

        System.out.println("登录成功！");

        while (true) {
            showMainMenu();
            System.out.println("请选择操作(0-6):");
            String choice = sc.nextLine();

            switch (choice) {
                case "1":
                    countStudents();
                    break;
                case "2":
                    listAllStudents();
                    break;
                case "3":
                    queryStudentById();
                    break;
                case "4":
                    queryStudentsByName();
                    break;
                case "5":
                    updateStudentBirthday();
                    break;
                case "6":
                    deleteStudent();
                    break;
                case "0":
                    System.out.println("系统退出，谢谢使用！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }
}
